package com.peas.platform.module.icims.base.auth.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Assert;
import com.peas.platform.common.core.configuration.RequestContextUtil;
import com.peas.platform.common.core.dto.ResponseDto;
import com.peas.platform.module.icims.base.auth.api.req.AddFavoriteVo;
import com.peas.platform.module.icims.base.auth.dto.PermissionDto;
import com.peas.platform.module.icims.base.auth.dto.UserFavoriteDto;
import com.peas.platform.module.icims.base.auth.service.PermissionService;
import com.peas.platform.module.icims.base.auth.service.UserFavoriteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "自定义接口-用户收藏")
@RestController
@RequestMapping("/api/v1/customUserFavorite")
@Slf4j
public class CustomUserFavoriteController {

    @Resource
    private HttpServletRequest request;
    @Resource
    private PermissionService permissionService;
    @Resource
    private UserFavoriteService userFavoriteService;

    private PermissionDto getPermissionByCode(String code) {
        List<PermissionDto> list = permissionService.listByField("permissionCode", code);
        Assert.notEmpty(list, "找不到菜单权限");
        Assert.isTrue(list.size() == 1, "数据错误");
        Assert.notBlank(code, "参数错误");
        return list.get(0);
    }

    @ApiOperation(value = "根据code添加用户收藏")
    @PostMapping(value = "/addFavorite")
    public ResponseDto<String> addFavorite(@RequestBody @Valid AddFavoriteVo vo) {
        PermissionDto pd = getPermissionByCode(vo.getCode());
        Long userId = Long.valueOf(RequestContextUtil.getCurrentUserId(true));
        UserFavoriteDto c = new UserFavoriteDto();
        c.setUserId(userId);
        c.setPermissionId(pd.getId());
        List<UserFavoriteDto> r = userFavoriteService.list(c);
        if (CollUtil.isEmpty(r)) {
            c.setTitle(vo.getTitle());
            c.setIcon(vo.getIcon());
            c.setName(vo.getName());
            c.setPath(vo.getPath());
        }
        c.setFavoriteTime(DateTime.now());
        c.setFavoriteIp(request.getRemoteAddr());
        userFavoriteService.save(c);
        return ResponseDto.getSuccessResponseDto("true");
    }

    @ApiOperation(value = "根据code删除用户收藏")
    @PostMapping(value = "/delFavorite/{code}")
    public ResponseDto<String> delFavorite(@PathVariable(value = "code") String code) {
        PermissionDto pd = getPermissionByCode(code);
        Long userId = Long.valueOf(RequestContextUtil.getCurrentUserId(true));
        UserFavoriteDto c = new UserFavoriteDto();
        c.setUserId(userId);
        c.setPermissionId(pd.getId());
        List<UserFavoriteDto> r = userFavoriteService.list(c);
        if (CollUtil.isNotEmpty(r)) {
            userFavoriteService.removeBatchByList(r);
        }
        return ResponseDto.getSuccessResponseDto("true");
    }

    @ApiOperation(value = "根据code查询用户收藏")
    @GetMapping(value = "/getFavorite/{code}")
    public ResponseDto<List<UserFavoriteDto>> getFavorite(@PathVariable(value = "code") String code) {
        PermissionDto pd = getPermissionByCode(code);
        Long userId = Long.valueOf(RequestContextUtil.getCurrentUserId(true));
        UserFavoriteDto c = new UserFavoriteDto();
        c.setUserId(userId);
        c.setPermissionId(pd.getId());
        List<UserFavoriteDto> r = userFavoriteService.list(c);
        return ResponseDto.getSuccessResponseDto(r);
    }

}
